#include<bits/stdc++.h>
using namespace std;
const int N = 20;
int a[N+1];
int m[N+1][N+1];
int v[N+1];
int n;
int ans;
int nn;
int cur[N+1];
int maxx[N+1];
void dfs(int i,int t){
	if(i>n)return;
	if(t>ans){
		nn = i;
		for(int j = 1;j<=nn;j++){
			maxx[j] = cur[j];
		}
		ans = t;
	}

	for(int j = 1;j<=n;j++){
		if(v[j]==1)continue;
		if(i!=1&&m[cur[i-1]][j]==0)continue;
		v[j] = 1;
		cur[i] = j;
		dfs(i+1,t+a[j]);
		v[j] = 0;
	}
}
int main(){

	cin>>n;
	for(int i = 1;i <= n;i++){
		cin>>a[i];
	}
	for(int i = 1;i < n;i++){
		for(int j = i+1;j <= n;j++){
			cin>>m[i][j];
			m[j][i] = m[i][j];
		}
	}
	dfs(1,0);
	for(int i = 1;i<=n;i++){
		cout<<maxx[i]<<" ";
	}
	cout<<endl;
	cout<<ans;
	return 0;
} 
